//		Written by Don Yacktman Copyright (c) 1995 by Don Yacktman.
//				Version 1.0.  All rights reserved.
//
//		This notice may not be removed from this source code.
//
//	This object is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	

#import "MiscClipTextField.h"
#import <misckit/misckit.h>

@implementation MiscClipTextField : TextField

#ifdef NOISYDEBUG
#define PRINT printf("TextField (width %f) set to:  \"%s\"  width %f.\n", \
		NX_WIDTH(&bounds), [temp stringValue], \
		[[self font] getWidthOf:[temp stringValue]])
#define PRINT1 printf("\n")
#else
#define PRINT 
#define PRINT1 
#endif

#define SLOP 18.0 // how many extra pixels of margin we need...
		// tweaked so that the fields _looks_ good.

- setStringValue:(const char *)aString
{
	id temp = [MiscString new];
	id mainFont = [self font];
	id scrnFont = [mainFont screenFont];
	id theFont = (scrnFont ? scrnFont : mainFont); // use screen font if exists

	[temp setStringValue:aString];
	PRINT;
	// Using ceil and floor makes sure that if we err, we do it in the
	// direction of safety.  (And we will never err more than a pixel...)
	// so I guess the SLOP is going to make more of a difference anyway.
	if (ceil([theFont getWidthOf:[temp stringValue]]) >
			floor((NX_WIDTH(&bounds) - SLOP))) { // too long?
		[temp insert:"..." at:0];
		while (ceil([theFont getWidthOf:[temp stringValue]]) >
				floor((NX_WIDTH(&bounds) - SLOP))) {
			[temp replaceFrom:3 length:1 with:""]; // pull out chars
			PRINT;
		}
		// now we should be down to a size that will fit.
	}
	PRINT1;
	return [super setStringValue:[temp stringValueAndFree]];
}

@end
